using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	#endregion
	public class CHUCNANGRepository
	{
		public static List<CHUCNANG> SelectAll()
		{
			return DataContext.Instance.CHUCNANG.OrderBy(item => item.TEN_CHUC_NANG).ToList();
		}

		public static CHUCNANG SelectByID(int maCHUCNANG)
		{
			return DataContext.Instance.CHUCNANG.FirstOrDefault(item => item.CHUCNANG_ID == maCHUCNANG );
		}

		public static bool Insert(CHUCNANG obj)
		{
			try
			{
				DataContext.Instance.CHUCNANG.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(int maCHUCNANG)
		{
			try
			{
				var delitem = DataContext.Instance.CHUCNANG.FirstOrDefault(item => item.CHUCNANG_ID == maCHUCNANG );
				DataContext.Instance.CHUCNANG.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<CHUCNANG> RetrieveByID(int maCHUCNANG)
		{
			return (from item in DataContext.Instance.CHUCNANG where  item.CHUCNANG_ID == maCHUCNANG  select item).ToList();
		}

	}
}